home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / basic / ace_final.lha / ACE_GPL_Release / prgs / Library / filepat.b < prev    next >
Encoding:
Text File  |  1996-08-29  |  1.7 KB  |  87 lines

  1. /*
  2. ** Get files/directories specified by a pattern.
  3. ** Recurse into directories if "all" is part of the pattern.
  4. **
  5. ** Author: David J Benn
  6. **   Date: 25th August 1996
  7. */
  8.  
  9. #include <dos/dos.h>
  10. #include <dos/dosasl.h>
  11. #include <funcs/dos_funcs.h>
  12.  
  13. DefLng a-z
  14.  
  15. Const BUFSZ = 1024
  16.  
  17. Sub Usage
  18.   Print "usage: ";Arg$(0);" file-pattern"
  19. End Sub
  20.  
  21. Sub Abort
  22. /*
  23. ** Quits but does no cleanup!
  24. */
  25.   Print Arg$(0);": ***break!"
  26.   Stop
  27. End Sub
  28.  
  29. Sub VisitFiles(pat$, allFiles)
  30. /*
  31. ** Visit each file specified by the pattern.
  32. ** If the pattern contains "all", this function
  33. ** is called recursively. Each recursive call
  34. ** requires a new AnchorPath structure variable
  35. ** so that previous pattern match states aren't
  36. ** affected. 
  37. */
  38. Declare Struct AnchorPath *anchor
  39. Declare Struct FileInfoBlock *fileInfo
  40.  
  41.   anchor = Alloc(Sizeof(AnchorPath)+BUFSZ)
  42.   If anchor = NULL Then 
  43.     Print Arg$(0);": Memory allocation error!"
  44.   Else
  45.     anchor->ap_StrLen = BUFSZ
  46.     result = MatchFirst(pat$, anchor)
  47.     While result <> ERROR_NO_MORE_ENTRIES
  48.       Print anchor->ap_Buf;
  49.       FileInfo = @anchor->ap_Info
  50.       If fileInfo->fib_DirEntryType > 0 Then
  51.         Print " [DIR]"
  52.         If allFiles Then Call VisitFiles(anchor->ap_Buf+"/#?", allFiles)
  53.       Else
  54.     Print
  55.       End If
  56.       result = MatchNext(anchor)
  57.     Wend
  58.     MatchEnd(anchor)
  59.   End If
  60. End Sub
  61.  
  62. /*
  63. ** Main.
  64. */
  65. If Argcount >= 1 Then
  66.   On Break Call Abort : Break On
  67.  
  68.   /*
  69.   ** Construct the pattern from potentially multiple arguments.
  70.   */
  71.   pat$ = ""
  72.   For i=1 To Argcount
  73.     pat$ = pat$ + Arg$(i)
  74.     If i <> Argcount Then pat$ = pat$ + " "
  75.   Next
  76.   If Ucase$(Right$(pat$,4)) = " ALL" Then
  77.     allFiles = TRUE
  78.     pat$ = Left$(pat$,Len(pat$)-4)
  79.   Else
  80.     allFiles = FALSE
  81.   End If
  82.  
  83.   VisitFiles(pat$, allFiles)
  84. Else
  85.   Usage
  86. End If
  87.